package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理")
@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
private CategoryService categoryService;

    /**
     * 分类管理的分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation("分类管理分页查询")
    @GetMapping("/page")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类管理分页查询，{}",categoryPageQueryDTO);
PageResult pageResult =  categoryService.page(categoryPageQueryDTO);
  return  Result.success(pageResult);

    }

    /**
     * 分类管理的新增
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类管理，{}",categoryDTO);
     categoryService.insertCategory(categoryDTO);
      return Result.success();
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("启用、禁用分类")
    @PostMapping("/status/{status}")
    public Result stopCategory(@PathVariable Integer status,Long id){
        log.info("启用、禁用分类:{}，{}",id,status);
        categoryService.stop(id,status);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @ApiOperation("删除分类")
    @DeleteMapping
public  Result deleteCategory(Long id){
    log.info("删除分类{}",id);
    categoryService.delete(id);
    return  Result.success();
    }

    /**
     * 类型查询
     * @param type
     * @return
     */
    @ApiOperation("类型查询")
    @GetMapping("/list")
    public Result <List<Category>> typeSelect(Integer type){
        log.info("类型查询{}",type);
    List<Category>  list =  categoryService.typeSelect(type);
        return Result.success(list);
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类{}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();

    }
}
